import App from './App'

// #ifndef VUE3
import Vue from 'vue'
import './uni.promisify.adaptor'
Vue.config.productionTip = false
App.mpType = 'app'
const app = new Vue({
	...App
})
app.$mount()
// #endif

// #ifdef VUE3
import { createSSRApp } from 'vue'
export function createApp() {
	const app = createSSRApp(App)
	return {
		app
	}
}
// #endif


function getOsName() {
	const systemInfo = uni.getSystemInfoSync();
	return systemInfo.platform;
}
const osName = getOsName();

function showMsg (msg) {
	console.info("信息提示", msg);
	if (osName === 'android' || osName === 'ios') {
		uni.showToast({
			title: msg,
			icon: 'none'
		});
	} else {
		alert(msg);
	}
}


//region 移动推送

function handleResult (action, result, callback) {
	console.info(`${action} 方法回调`, result);
	if (typeof callback === "function") {
		callback(result);
	}
}

try {
	if (osName === 'android' || osName === 'ios') {
		showMsg("当前OS为App：" + osName);
		
		const aliyunPush = uni.requireNativePlugin('Aliyun-Push');
		console.info("aliyunPush", aliyunPush);


		function getDeviceId() {
			const result = aliyunPush.getDeviceId();
			handleResult("getDeviceId", result, function() {
				showMsg("deviceId：" + result.data.deviceId);
			});
			return result;
		}
		
		function doGetDeviceId() {
			try {
				return getDeviceId().data.deviceId;
			} catch (e) {
				console.error("获取设备ID失败:", e);
				return null;
			}
		}

		if (osName === 'android') {
			// 通知通道创建
			const channel = uni.requireNativePlugin('Aliyun-Push-NotificationChannel');
			console.info("channel", channel);
			channel.createChannel({
				id: 'aaa',
				name: '测试通道A',
				desc: '测试创建通知通道',
				importance: 3,
			});

			// 注册推送
			aliyunPush.registerPush({}, result => {
				const event = result.event;
				if (event === 'registerPush') {
					if (result.code === 'success') {
						console.log("注册推送 成功", result);
						console.log("设备号：", result.data.deviceId);
					} else {
						console.log("注册推送 失败", result);
						console.log("注册推送 失败", "code: " + result.code, "msg: " + result.msg);
					}
					console.log("设备ID", doGetDeviceId());
				} else if (event === 'onMessage') {
					console.log("接收消息", result);
					
					const data = JSON.stringify(result.data);
					console.log("接收到消息数据（在线消息): " + data);
				} else {
					console.log("未知消息", result);
				}
			});

			// 注册厂商通道
			const aliyunThirdPush = uni.requireNativePlugin('Aliyun-ThirdPush');
			aliyunThirdPush.registerThirdPush({}, result => {
				const data = JSON.stringify(result);
				console.log("接收厂商消息: " + data);
			});
		} else if (osName === 'ios') {
			const aliyunPush = uni.requireNativePlugin('Aliyun-Push');
			
			// 注册前台推送通知的回调接口
			aliyunPush.setNotificationCallback({}, result => {
				handleResult("setNotificationCallback", result);
			});
			
			// 注册用户点击推送通知的回调接口
			aliyunPush.setNotificationResponseCallback({}, result => {
				handleResult("setNotificationResponseCallback", result);
			});
			
			// 注册推送消息的回调接口
			aliyunPush.setMessageCallback({}, result => {
				handleResult("setMessageCallback", result);
			});
		}
	} else {
		showMsg("当前OS不是App：" + osName);
	}
} catch (e) {
	console.error("初始化AliyunPush失败：", e);
}

//endregion
